草庐IT

pointers - 指针问题

全部标签

arrays - Golang 数组指针

我想阐明Golang中的这种行为,为什么当您在数组上获取内存引用并更改此引用的值时,引用数组中没有任何变化。一个例子:vart[5]intprintType(t,"t")p:=&tprintType(p,"p")x:=*px[0]=4printType(x,"x")printType(p,"p")printType(t,"t")这段代码返回[t]Type:[5]intKind:arrayAdr:%!p([5]int=[00000])Value:([00000])[p]Type:*[5]intKind:ptrAdr:0xc4200142d0Value:(&[00000])[x]Type:

go - 如何在 Go 中使用反射将结构的 nil 指针设置为结构的零值?

我正在递归地抓取结构。它与json包的作用相同。如果遇到指向结构的nil指针,则应将指针设置为结构的零值,以便能够继续挖掘。我怎样才能做到这一点?varunmarshalfunc(sreflect.Value)errorunmarshal=func(sreflect.Value)error{t:=s.Type()fori:=0;i 最佳答案 您可以使用reflect.New(f.Type.Elem())创建一个指向零值的指针,然后使用v.Set(value)来设置它。根据反射(reflect)文档:New返回一个值,表示指向指定类型

golang 包执行问题

我在使用goexec包时遇到问题。我收到这个错误:'\"C:\ProgramFiles\SublimeText3\sublime_text.exe\"'isnotrecognizedasaninternalorexternalcommand,operableprogramorbatchfile.当我尝试在Windows机器上用golang运行这段代码时:packagemainimport("os/exec""fmt")funcmain(){varCMDstring="\"C:\\ProgramFiles\\SublimeText3\\sublime_text.exe\"C:\\User

pointers - 在 golang 中返回一个指针

我是go的初学者,但我有一个问题:我有以下代码:packagelabimport("fmt""math")typeCirclestruct{xfloat64yfloat64rfloat64}func(c*Circle)area()float64{returnmath.Pi*c.r*c.r}funcStructCode(){c:=Circle{1,2,5}fmt.Println("structaddr",c)fmt.Println("Circle",c.area())}我的问题是,Circleareafunction接受一个CirclePointer并返回面积。基于此。为什么当我打印结构

go - 从嵌套 block 返回,范围问题

基本概念我正在学习Go,但我不明白如何从函数的内部block返回数据。packagemainimport("fmt")funcmain(){fmt.Println("Hello,playground")ifisTrue("asasa"){fmt.Println("isTrueistrue")}}funcisTrue(sstring)bool{ifs=="asasa"{fmt.Println("itmatches")returntrue}}https://play.golang.org/p/x6l29CNdg-o我应该如何返回在嵌套(甚至多次嵌套)block内满足的条件?真实案例场景fu

go - 试图将文字转换为 Golang 中的指针

为了简化我负责测试的代码的测试,我正在重构我团队的LogHandler。他们有这样的事情:typeLogHandlerStstruct{pathstringfileNamestringprojectNamestring}var(//InstanceTheprojectinstanceofloghandler.InstanceLogHandlerSt//OnResponseErrorAfunctionthatcanbesettobecalledonresponseerror.OnResponseErrorfunc(contextinterface{},response*http.Resp

go - switch 语句中的可变范围问题

我有一个程序可以解析日志文件并返回一个结构片段,其中包含来自文件的填充数据。我还编写了一个函数来将结构项添加到上述列表中。但有一个错误提示“无法使用‘sf’(类型*SegmentationFault)作为SegmentationFault类型”,该错误源于此函数。我该如何解决这个问题?func(sfList*SegmentationFaultList)AddItem(itemSegmentationFault)[]SegmentationFault{sfList.Items=append(sfList.Items,item)returnsfList.Items}funcparseLog

arrays - 从数组结构呈现 json 的问题

这个问题在这里已经有了答案:JSONanddealingwithunexportedfields(2个答案)关闭4年前。我用gorilla/mux和mysql数据库做一个简单的休息服务typeCarrostruct{Anoint`json:"ano"`Corstring`json:"cor"`Nomestring`json:"nome""`}typeRevendastruct{carro[]Carrorodastring}functest(whttp.ResponseWriter,r*http.Request){varlistas[]CarrocarA:=Carro{1975,"Ama

go - 在go中将Slice指针转换为Slice

我有一些方法可以返回slice指针中的数据,现在我必须将其转换为slice数组。如何将slice指针转换为slice数组。peerRoundState,err:=s.nodeview.PeerRoundStates()fmt.Println("Thisreturnvalueisslicepointer",peerRoundState)iferr!=nil{returnnil,err}//PeerRoundStatesthisistypeofslice.return&ConsensusResponse{RoundState:s.nodeview.RoundState().RoundSta

c - 直接C指针转换

我有这个C代码:uint8_t*data[BUF_SIZE];data=...;//externvoidgoReadData(uint8_t*data,intbufferSize);goReadData(data,BUF_SIZE)在GO代码中,我试图将data指针用作GO数组或slice,我想从*C.uint8_t中检索一个[]uint8。我知道data的大小//exportgoReadDatafuncgoReadData(data*C.uint8_t,bufferSizeC.int){fmt.Printf("Datatype%v\n",reflect.TypeOf(data))//